package com.fe.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fe.model.Deal;
import com.fe.model.Patriarch;
import com.fe.model.Teacher;
import com.fe.service.DealService;
import com.fe.util.JacksonData;
import com.fe.util.PageBean;
@Controller
@RequestMapping("/deal")
public class DealController {
	
	@Autowired
	private DealService dealService;
	
	/**
	 * 
	 * 方法描述：跳转到listDeal方法
	 * @return
	 * @author ssh
	 * @date 2018年9月21日 下午10:32:41
	 */
	@GetMapping("/addDeal")
	public String addDealGet() {
		return "redirect:/deal/listDeal";
	}
	
	/**
	 * 
	 * 方法描述：添加订单
	 * @param request
	 * @param map
	 * @param deal
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午4:27:00
	 */
	@PostMapping("/addDeal")
	public String addDeal(HttpServletRequest request, Map<String, Object> map, Deal deal) {
		String message = "";
		try {
			dealService.addDeal(request, map, deal);
		} catch (Exception e) {
			message = e.getMessage();
		}
		return "forward:/deal/listDeal?message="+message;
	}
	
	/**
	 * 
	 * 方法描述：根据id获取所有订单
	 * @param deal
	 * @param request
	 * @param map
	 * @param pages
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午7:26:23
	 */
	@RequestMapping("/listDeal")
	public String listDeal(Deal deal, HttpServletRequest request, Map<String, Object> map, @RequestParam(required=false,defaultValue="1") int pages,String message) {
		try {
			Deal deal2 = new Deal();
			//根据登陆的账号，分别赋值
			HttpSession session = request.getSession();
			if (null != session.getAttribute("teacher")) {
				Teacher teacher = (Teacher) session.getAttribute("teacher");
				deal.setTea_id(teacher.getTea_id());
				deal2.setTea_id(teacher.getTea_id());
			}else {
				Patriarch patriarch = (Patriarch) session.getAttribute("patriarch");
				deal.setPat_id(patriarch.getPat_id());
				deal2.setPat_id(patriarch.getPat_id());
			}
			if(message != null && message != "") {
				map = PageBean.serverMap(map, deal2, pages);
				map.put("message", message);
			}else {
				map = PageBean.serverMap(map, deal, pages);
			}
			dealService.listDeal(map);
			map = PageBean.map(map, pages, request);
		} catch (Exception e) {
			map.put("message", e.getMessage());
		}
		return "deal/listDeal";
	}
	
	/**
	 * 
	 * 方法描述：更新订单中的评论，状态
	 * @param deal
	 * @return
	 * @author ssh
	 * @date 2018年8月26日 下午2:23:15
	 */
	@PostMapping("/updateDeal")
	@ResponseBody
	public JacksonData updateDeal(Deal deal) {
		JacksonData jd = new JacksonData();
		try {
			dealService.updateDeal(deal);
			jd.success();
		} catch (Exception e) {
			jd.failure(e.getMessage());
		}
		return jd;
	}
}
